﻿using CommandLine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DbTeleportáció
{
    /// <summary>
    /// Main entry point for this application
    /// </summary>
    /// <remarks>
    /// -------------------------------------------Change Log---------------------------------------------
    /// 04/01/2016 - DCS - Created
    /// --------------------------------------------------------------------------------------------------
    /// </remarks>
    class Program
    {
        /// <summary>
        /// Main entry method for the application
        /// </summary>
        /// <param name="args">Collection of command-line arguments supplied when this program was started</param>
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Environment.ExitCode = 0;

            var options = new Options();
            var parser = new Parser();

            WriteApplicationInformation();

            if (!parser.ParseArguments(args, options))
            {
                Environment.ExitCode = (int)ExitCodes.InvalidCommandLineArguments;
                Console.WriteLine(options.GetUsage());
            }
            else
            {
                var fileSystemManager = new FileMgr(options);

                fileSystemManager.CreateFileDropDirectory();
                fileSystemManager.ClearFileDropDirectory();

                var schema = new SchemaExporter(options);
                schema.ExportSchema();

                var exporter = new TableExporter(options);
                exporter.ExportTableData();

                if (!options.SkipCompression)
                {
                    fileSystemManager.CompressExtractedData();
                    fileSystemManager.DeleteUncompressedFiles();
                }

                Console.WriteLine("Export completed.  Press any key to exit...");
            }

            if (options.WaitToExit)
            {
                Console.ReadKey();
            }

            Environment.Exit(Environment.ExitCode);
        }

        /// <summary>
        /// Application-level error handler
        /// </summary>
        /// <param name="sender">Object that produced the unhandled exception</param>
        /// <param name="e">Object containing information about the unhandled exception that occurred</param>
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var exception = e.ExceptionObject as Exception;

            Console.WriteLine("An error occurred attempting to complete an operation.");

            if (Environment.ExitCode == (int)ExitCodes.Success)
            {
                Environment.ExitCode = (int)ExitCodes.UnexpectedException;
            }

            if (exception != null)
            {
                Console.WriteLine(exception.Message);
                Console.WriteLine(exception.ToString());
                Console.WriteLine(exception.StackTrace);
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        /// <summary>
        /// Writes a header that includes basic information about this application
        /// </summary>
        private static void WriteApplicationInformation()
        {
            Assembly currentAssembly = Assembly.GetExecutingAssembly();

            Console.WriteLine("*******************************************************************");
            Console.WriteLine($"* Database Transporter Version {currentAssembly.GetName().Version.ToString()}");
            Console.WriteLine("*******************************************************************");
        }
    }
}
